#!/bin/sh

#之前有写编译armbian内核模块 ，但是在有些情况下模块并不能被加载。如我要开启bbr，虽然系统自带了bbr模块，但是没bbr所需要的fq队列模块，编译fq模块后不能加载，因为内核在编译的时候未开启NET_SCHED，遇到这种情况只有重新编译内核了。

#作者：哥的迷思
#链接：https://www.jianshu.com/p/5e20e751f820
#来源：简书
#著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。

losetup -P -f --show Armbian_5.62_Aml-s9xxx_Ubuntu_bionic_default_4.18.7_20181012.img 
#loop3为上面挂载的loop设备
mount /dev/loop3p2 /mnt/
mount /dev/loop3p1 /mnt/boot/

yum -y install binutils-aarch64-linux-gnu.x86_64 gcc-c++-aarch64-linux-gnu.x86_64 gcc-c++-aarch64-linux-gnu.x86_64 uboot-images-armv8.noarch 

apt-get install gcc make pkg-config git bison flex libelf-dev libssl-dev libncurses5-dev libncursesw5-dev
wget https://releases.linaro.org/components/toolchain/binaries/latest-7/aarch64-linux-gnu/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.xz
https://publishing-ie-linaro-org.s3.amazonaws.com/releases/components/toolchain/binaries/latest-7/aarch64-linux-gnu/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu.tar.xz?Signature=%2BBdLOxoM%2FNd5bt49n%2B0p%2FrOZirQ%3D&Expires=1658504500&AWSAccessKeyId=AKIAIELXV2RYNAHFUP7A

wget https://publishing-ie-linaro-org.s3.amazonaws.com/releases/components/toolchain/binaries/latest-7/aarch64-linux-gnu/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu.tar.xz?Signature=4wao%2BLLgIGTxBSUuX1ftwnpZBcM%3D&Expires=1658504603&AWSAccessKeyId=AKIAIELXV2RYNAHFUP7A



tar -Jxvf gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu.tar.xz 
export ARCH=arm64 
export CROSS_COMPILE=/root/gcc-linaro-7.3.1-2018.05-x86_64_aarch64-linux-gnu/bin/aarch64-linux-gnu-
git clone https://github.com/150balbes/Amlogic_s905-kernel.git
#4.9内核，gcc要使用版本6
#git clone -b khadas-4.9 https://github.com/150balbes/Amlogic_s905-kernel.git
cd Amlogic_s905-kernel
#使用4.18.7内核
git checkout 20181012

make clean
make menuconfig

make LOCALVERSION="-aml-s9xxx" Image -j 4
make install INSTALL_PATH=/mnt/boot/
cp arch/arm64/boot/Image /mnt/boot/zImage

rm -rf /mnt/lib/modules
make LOCALVERSION="-aml-s9xxx" modules -j 4
make modules_install INSTALL_MOD_PATH=/mnt/



rm -rf /mnt/boot/dtb.img
wget https://github.com/yangxuan8282/phicomm-n1/releases/download/dtb/meson-gxl-s905d-phicomm-n1.dtb -O /mnt/boot/dtb.img

我在编译树莓派的时候也遇到同样的问题，后来发现是make menuconfig 的时候忘了带环境变量。
正确的方式应该是 make ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- menuconfig